package ui.fragment.group_management;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.group_management.PhotoAndVDOTabInGroupFeedFragmentAdapter;
import controller.company.SelectCompanyAsync;
import controller.group_management.GroupManagementAsync;
import controller.group_management.PhotoAndVDOTabInGroupFeedFragmentAsync;
import model.group_management.PhotoAndVDOTabInGroupFeedItem;
import ui.activity.group_management.GroupFeed;
import ui.activity.login.Login;

public class PhotoAndVDOTabInGroupFeedFragment extends Fragment
        implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener{

    /**
     * String section
     */

    /**
     * View section
     */
    public static ListView mLvInPhotoAndVDOInGroupFeedFragment;
    public static PhotoAndVDOTabInGroupFeedFragmentAdapter photoAndVDOTabInGroupFeedFragmentAdapter;
    private static LinearLayout mLlNoContentInPhotoAndVDOInGroupFeedFragment;
    private SwipeRefreshLayout mSrlInPhotoAndVDOInGroupFeedFragment;
    /**
     * Others section
     */

    /**
     * Listener section
     */
    public static Fragment newInstance() {
// TODO transfer position of group_id on here
        PhotoAndVDOTabInGroupFeedFragment fragment = new PhotoAndVDOTabInGroupFeedFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_photo_and_vdo_tab_in_group_feed_fragment, container, false);
        // Initial views
        initialViews(v);
        initialData();
        setDataForPhotoAndVDOTabInGroupFeedFragment();
        return v;
    }

    @Override
    public void onRefresh() {
        // what's thing need refresh when pull down, need place in here
        try {
            //Call new thread to get Data
            Log.i("", "refresh data");
            if (!PhotoAndVDOTabInGroupFeedFragmentAsync.
                    mAlPhotoAndVDOTabInGroupFeed.isEmpty())
                PhotoAndVDOTabInGroupFeedFragmentAsync.
                        mAlPhotoAndVDOTabInGroupFeed.clear();
            photoAndVDOTabInGroupFeedFragmentAdapter.notifyDataSetChanged();
            setDataForPhotoAndVDOTabInGroupFeedFragment();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // what's thing need refresh when pull down, need place in here
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    // Stop the animation once we are done fetching data.
                    mSrlInPhotoAndVDOInGroupFeedFragment.setRefreshing(false);
                    /**
                     * You can add code to update your list with the new data.
                     **/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 4000);
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        photoAndVDOTabInGroupFeedFragmentAdapter = new PhotoAndVDOTabInGroupFeedFragmentAdapter(getActivity(),
                R.layout.simple_list_item_photo_and_vdo_tab_in_group_feed_fragment,
                PhotoAndVDOTabInGroupFeedFragmentAsync.mAlPhotoAndVDOTabInGroupFeed);
        mLvInPhotoAndVDOInGroupFeedFragment.setAdapter(photoAndVDOTabInGroupFeedFragmentAdapter);
        mSrlInPhotoAndVDOInGroupFeedFragment.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mSrlInPhotoAndVDOInGroupFeedFragment.setOnRefreshListener(this);
    }

    private void initialViews(View v) {
        mLvInPhotoAndVDOInGroupFeedFragment = (ListView) v.findViewById(
                R.id.lv_in_photo_and_vdo_tab_in_group_feed_fragment);
        mLlNoContentInPhotoAndVDOInGroupFeedFragment = (LinearLayout) v.findViewById(
                R.id.ll_no_content_in_photo_and_vdo_tab_in_group_feed_fragment);
        mSrlInPhotoAndVDOInGroupFeedFragment = (SwipeRefreshLayout) v.findViewById(
                R.id.srl_in_photo_and_vdo_tab_in_group_feed_fragment);
    }

    /**
     * The others methods
     */

    private void setDataForPhotoAndVDOTabInGroupFeedFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new PhotoAndVDOTabInGroupFeedFragmentAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.groupManagement.getGroupIdSelected()),
                    getActivity().getString(R.string.files),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    "0");
        else
            new PhotoAndVDOTabInGroupFeedFragmentAsync(getActivity()).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.groupManagement.getGroupIdSelected()),
                    getActivity().getString(R.string.files),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    "0");
    }

    public static void displayViewForPhotoAndVDOTabInGroupFeedFragment (ArrayList<PhotoAndVDOTabInGroupFeedItem> mAlPhotoAndVDOTabInGroupFeed) {
        if (mAlPhotoAndVDOTabInGroupFeed.size() > 0 ) {
            mLvInPhotoAndVDOInGroupFeedFragment.setVisibility(View.VISIBLE);
            mLlNoContentInPhotoAndVDOInGroupFeedFragment.setVisibility(View.GONE);
        } else {
            mLvInPhotoAndVDOInGroupFeedFragment.setVisibility(View.GONE);
            mLlNoContentInPhotoAndVDOInGroupFeedFragment.setVisibility(View.VISIBLE);
        }
    }

}
